package cn.kgc.ywz.smbms.web.web;

import cn.kgc.ywz.smbms.web.entity.Address;
import cn.kgc.ywz.smbms.web.entity.Role;
import cn.kgc.ywz.smbms.web.entity.User;
import cn.kgc.ywz.smbms.web.mapper.RoleMapper;
import cn.kgc.ywz.smbms.web.service.AddressService;
import cn.kgc.ywz.smbms.web.service.RoleService;
import cn.kgc.ywz.smbms.web.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class LinkageController {

    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private AddressService addressService;

    @RequestMapping("/toLinkage")
    public String toLinkage(Model model) {
        List<Role> roleList = this.roleService.findRole();
        model.addAttribute("roleList", roleList);
        return "linkage";
    }

    @RequestMapping("/doUser")
    @ResponseBody
    public Object doUser(Long roleId) {
        System.out.println("roleId = " + roleId);
        List<User> userList = this.userService.findByRoleId(roleId);
        return userList;
    }

    @RequestMapping("/doAddress")
    @ResponseBody
    public Object doAddress(Long userId) {
        System.out.println("userId = " + userId);
        List<Address> addressList = this.addressService.findByUserId(userId);
        for (Address address : addressList) {
            System.out.println("address = " + address);
        }
        return addressList;
    }
}
